##=============================================================================
## Appendix Table 11
##=============================================================================

##-----------------
# clear environment
rm(list=ls())
options(stringsAsFactors = FALSE, scipen = 999)
# source("R/functions.R")

##--------
# Set seed
seed <- sample.int(.Machine$integer.max, 1)
set.seed(seed)

##-------------
# Load Packages
ipak <- function(pkg){new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
if(length(new.pkg)) install.packages(new.pkg, dependencies = TRUE)
sapply(pkg, require, character.only = TRUE)
}

packages <- c("tidyverse", "infer")

ipak(packages)

##---------
# Load Data
load("~/Desktop/clean_ukraine_data.RData")

# Russian language at home
samp1 <- dat %>% 
  filter(home_lang == 2) %>% 
  mutate(survival_binary = ifelse(survival_mindset_z > 0.71, 1, 0)) %>% 
  mutate(survival_binary = factor(survival_binary, 
                                  labels = c("Low", "High"))) %>%
  select(id, survival_binary, ukr_no_nato, rus_lang_schools)

samp1 %>%
  t_test(formula = ukr_no_nato  ~ survival_binary, 
         alternative = "two-sided", order = c("Low", "High"))

samp1 %>%
  t_test(formula = rus_lang_schools  ~ survival_binary, 
         alternative = "two-sided", c("Low", "High"))

# Russian, both, hard to say
samp2 <- dat %>% 
  filter(home_lang == 2 | home_lang == 3 | home_lang == 7) %>% 
  mutate(survival_binary = ifelse(survival_mindset_z > 0.71, 1, 0)) %>% 
  mutate(survival_binary = factor(survival_binary, 
                                  labels = c("Low", "High"))) %>%
  select(id, survival_binary, ukr_no_nato, rus_lang_schools)

samp2 %>%
  t_test(formula = ukr_no_nato  ~ survival_binary, 
         alternative = "two-sided", order = c("Low", "High"))

samp2 %>%
  t_test(formula = rus_lang_schools  ~ survival_binary, 
         alternative = "two-sided", c("Low", "High"))

# Ethnic Russian respondents only
samp3 <- dat %>% 
  filter(ethnic == 2) %>% 
  mutate(survival_binary = ifelse(survival_mindset_z > 0.71, 1, 0)) %>% 
  mutate(survival_binary = factor(survival_binary, 
                                  labels = c("Low", "High"))) %>%
  select(id, survival_binary, ukr_no_nato, rus_lang_schools)

samp3 %>%
  t_test(formula = ukr_no_nato  ~ survival_binary, 
         alternative = "two-sided", order = c("Low", "High"))

samp3 %>%
  t_test(formula = rus_lang_schools  ~ survival_binary, 
         alternative = "two-sided", c("Low", "High"))

rm(list = ls())
##=============================================================================
## End of File
##=============================================================================